﻿//includes for mail send function
using System.Net;
using System.Net.Mail;

//includes for md5 hashing function
using System.Security.Cryptography;
using System.Text;
using System;
using final_darskade.Models;
using System.Linq;

namespace final_darskade
{
    public class Helper
    {

        public static void sendMail(string To, string Subject, string message)
        {
            string username = "darskade@gmail.com";
            NetworkCredential userpass = new NetworkCredential(username, "se2.cdarskade");
            SmtpClient mail = new SmtpClient("smtp.gmail.com", 587);
            mail.Credentials = userpass;
            mail.EnableSsl = true;
            mail.Timeout = 100000;
            mail.Send(new MailMessage(username, To, Subject, message));
        }

        //private MD5
        public static string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        // Verify a hash against a string.
        public static bool verifyMd5Hash(string input, string hash)
        {
            // Hash the input.
            string hashOfInput = getMd5Hash(input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public static darskadeEntities database = new darskadeEntities();

        public static bool checkPrivilege(int userid, string privilege, int klassid)
        {
            var query = from row in database.privileges
                        where row.privilege_name.Equals( privilege)
                        select row;

            int privilegeid = query.First().privilege_id;
            var query1 = from row in database.klass_members
                        where row.klass_id == klassid &&
                              row.member_id == userid &&
                              row.privilege_id == privilegeid
                        select row;

            if (query1.Any() == true) return true; 

            var query2=from row in database.users
                       where row.user_id==userid
                       select row.role_id;
            int roleid=query2.First();

            /*var query3 = from row in database.privileges_in_role
                         where row.privilege_id == privilegeid &&
                               row.role_id == roleid
                         select row;
            if (query3.Any() == true)
                return true;


                              */
            return false;
        }
    }
}